Code
import pandas as pd
import plotly.graph_objects as go
# Load the data
df_weight = pd.read_csv("unicef_indicator_2.csv")
df_meta = pd.read_csv("unicef_metadata.csv")import pandas as pd
import plotly.graph_objects as go
# Load the data
df_weight = pd.read_csv("unicef_indicator_2.csv")
df_meta = pd.read_csv("unicef_metadata.csv")This analysis explores the relationship between childhood obesity (measured by Weight-for-Height) and life expectancy across different countries from 2000 to 2022. The aim is to uncover global trends in both childhood obesity and life expectancy, examining how these two indicators have evolved over time and understanding the factors that may influence them.
By analyzing these trends, the goal is to highlight the global issue of childhood obesity and its impact on future health outcomes, emphasizing the need for interventions that address both public health and nutrition in different countries.
This interactive plot compares Weight-for-Height (children’s weight) and Life Expectancy (in years) for different countries over time (2000–2022). The plot shows the trend of Weight-for-Height on the left y-axis (in thousands) and Life Expectancy on the right y-axis. Users can select any country from the dropdown to explore how these two indicators evolve over time.
# Filter weight indicator
df_weight = df_weight[df_weight["indicator"].str.contains("Weight-for-height", na=False)]
df_weight = df_weight[["country", "time_period", "obs_value"]].rename(columns={
"country": "Country",
"time_period": "Year",
"obs_value": "Weight"
})
# Filter life expectancy
df_meta = df_meta[["country", "year", "Life expectancy at birth, total (years)"]].rename(columns={
"country": "Country",
"year": "Year",
"Life expectancy at birth, total (years)": "LifeExpectancy"
})
# Merge
df = pd.merge(df_weight, df_meta, on=["Country", "Year"])
df = df.sort_values(["Country", "Year"])
# Get all countries
countries = df["Country"].unique()
# Create traces (2 per country)
traces = []
buttons = []
for i, country in enumerate(countries):
subset = df[df["Country"] == country]
visible = [False] * (2 * len(countries))
visible[2*i] = True
visible[2*i+1] = True
# Weight
traces.append(go.Scatter(
x=subset["Year"],
y=subset["Weight"],
name="Weight-for-height",
mode="lines+markers",
line=dict(color='deeppink'),
visible=(i == 0),
yaxis="y1"
))
# Life Expectancy
traces.append(go.Scatter(
x=subset["Year"],
y=subset["LifeExpectancy"],
name="Life Expectancy",
mode="lines+markers",
line=dict(color='firebrick', dash='dash'),
visible=(i == 0),
yaxis="y2"
))
# Dropdown button
buttons.append(dict(
label=country,
method="update",
args=[
{"visible": visible},
{"title": f"Weight vs Life Expectancy – {country}"}
]
))
# Layout
layout = go.Layout(
title=f"Weight vs Life Expectancy – {countries[0]}",
xaxis=dict(title="Year of Time Period"),
yaxis=dict(
title="Weight-for-height (Thousands)",
titlefont=dict(color="deeppink"),
tickfont=dict(color="deeppink")
),
yaxis2=dict(
title="Life Expectancy (Years)",
titlefont=dict(color="firebrick"),
tickfont=dict(color="firebrick"),
overlaying="y",
side="right"
),
updatemenus=[dict(
buttons=buttons,
direction="down",
showactive=True,
x=1.15,
xanchor="left",
y=1,
yanchor="top"
)],
legend=dict(x=0.01, y=0.99),
height=500
)
# Create figure
fig = go.Figure(data=traces, layout=layout)
fig.show()This interactive map visualizes the Global Weight-for-Height data for children under 5, specifically for those who fall into the >+2 SD (standard deviation) category, which indicates overweight children. The map spans from 2000 to 2022 and provides insights into the global distribution of overweight children over the years.
import plotly.express as px
map_df = df_weight.copy()
# Remove nulls
map_df = map_df.dropna(subset=["Weight", "Country", "Year"])
# Use the animation_frame feature to allow year selection
fig_map = px.choropleth(
map_df,
locations="Country",
locationmode="country names",
color="Weight",
hover_name="Country",
animation_frame="Year",
color_continuous_scale="YlOrRd",
title="Global Weight-for-height (>+2 SD) in Children Under 5",
labels={"Weight": "Children (Thousands)"}
)
# Improve layout
fig_map.update_layout(
margin={"r":0,"t":50,"l":0,"b":0},
geo=dict(showcoastlines=True, showland=True, showcountries=True),
)
# Show the map
fig_map.show()This interactive scatter plot compares Life Expectancy at birth (in years) and the number of Overweight Children (in thousands) across different countries. The data spans from 2000 to 2022, and for each year, both the data points and regression lines are displayed, providing insights into the evolving relationship between life expectancy and childhood obesity.
# --- Plot 3: Scatterplot with Year Dropdown and Regression Line ---
import statsmodels.api as sm
# Merge data for scatterplot
df_scatter = pd.merge(df_weight, df_meta, on=["Country", "Year"])
df_scatter = df_scatter.dropna(subset=["Weight", "LifeExpectancy"])
# Get available years
years = sorted(df_scatter["Year"].unique())
# Create traces and dropdown buttons
scatter_traces = []
scatter_buttons = []
for i, year in enumerate(years):
df_year = df_scatter[df_scatter["Year"] == year].copy()
df_year_sorted = df_year.sort_values("LifeExpectancy")
# Regression model
X = sm.add_constant(df_year_sorted["LifeExpectancy"])
model = sm.OLS(df_year_sorted["Weight"], X).fit()
reg_line = model.predict(X)
# Scatter trace
scatter = go.Scatter(
x=df_year_sorted["LifeExpectancy"],
y=df_year_sorted["Weight"],
mode="markers",
marker=dict(size=10, color='blue', opacity=0.6),
name=f"{year} Data",
text=df_year_sorted["Country"],
hovertemplate="<b>%{text}</b><br>Life Expectancy: %{x}<br>Overweight (000s): %{y}<extra></extra>",
visible=(i == 0)
)
scatter_traces.append(scatter)
# Regression line trace
reg_trace = go.Scatter(
x=df_year_sorted["LifeExpectancy"],
y=reg_line,
mode="lines",
line=dict(color="firebrick", dash="dash"),
name=f"{year} Regression",
visible=(i == 0)
)
scatter_traces.append(reg_trace)
# Dropdown button setup
vis = [False] * (2 * len(years))
vis[2 * i] = True
vis[2 * i + 1] = True
scatter_buttons.append(dict(
label=str(year),
method="update",
args=[
{"visible": vis},
{"title": f"Life Expectancy vs Overweight Children – {year}"}
]
))
# Layout for scatter plot
scatter_layout = go.Layout(
title=dict(
text=f"Life Expectancy vs Overweight Children – {years[0]}",
y=0.92,
x=0.5,
xanchor="center",
yanchor="top"
),
xaxis=dict(title="Life Expectancy at Birth (Years)"),
yaxis=dict(title="Overweight Children (Thousands)"),
updatemenus=[dict(
active=0,
buttons=scatter_buttons,
direction="down",
showactive=True,
x=0.01,
xanchor="left",
y=1.05,
yanchor="top"
)],
legend=dict(
x=0.01,
y=0.98,
bgcolor='rgba(255,255,255,0.7)',
bordercolor='gray',
borderwidth=1
),
margin=dict(t=100),
height=500
)
# Final scatter plot
fig_scatter = go.Figure(data=scatter_traces, layout=scatter_layout)
fig_scatter.show()This interactive bar chart visualizes the Top 10 Countries with the highest number of overweight children (measured in thousands) for each year, from 2000 to the most recent available year. The data is grouped by year and country, with the weight-for-height values (children’s weight) summed up for each country.
import plotly.express as px
# --- Plot 4: Interactive Bar Chart with Different Colors for Top 10 Countries by Year ---
# Prepare the data
df_top_countries = df.groupby(['Year', 'Country'], as_index=False)['Weight'].sum()
# Define a color palette
color_palette = px.colors.qualitative.Set2 # You can choose other palettes like "Set1", "Set3", etc.
# Create traces for each year
bar_traces = []
buttons = []
years = sorted(df_top_countries['Year'].unique())
for i, year in enumerate(years):
df_year = df_top_countries[df_top_countries["Year"] == year]
# Sort the countries by Weight (overweight children)
df_year_sorted = df_year.sort_values(by="Weight", ascending=False).head(10)
# Create bar chart for each year with different colors for top 10 countries
bar_trace = go.Bar(
x=df_year_sorted["Country"],
y=df_year_sorted["Weight"],
name=str(year),
marker=dict(color=color_palette[:len(df_year_sorted)]), # Apply different colors
visible=(i == 0)
)
bar_traces.append(bar_trace)
# Button for dropdown
visibility = [False] * len(years)
visibility[i] = True
buttons.append(dict(
label=str(year),
method="update",
args=[{"visible": visibility},
{"title": f"Top 10 Countries by Overweight Children in {year}",
"xaxis": {"title": "Country"},
"yaxis": {"title": "Overweight Children (Thousands)"}}]
))
# Layout
layout = go.Layout(
title=f"Top 10 Countries by Overweight Children in {years[0]}",
updatemenus=[dict(
buttons=buttons,
direction="down",
showactive=True,
x=0.01,
xanchor="left",
y=1.15,
yanchor="top"
)],
xaxis=dict(title="Country"),
yaxis=dict(title="Overweight Children (Thousands)"),
height=500,
margin=dict(t=100, b=40, l=60, r=20)
)
# Final bar chart with traces
fig_bar = go.Figure(data=bar_traces, layout=layout)
fig_bar.show()This plot shows the Weight-for-Height (children’s weight) across the Top 5 Countries over time (2000–2020). The top 5 countries are China, Egypt, India, Indonesia, and the United States, and the data is measured in thousands of children.
# Step 1: Calculate total weight-for-height for each country
total_weight_by_country = df.groupby("Country")["Weight"].sum().reset_index()
# Step 2: Sort countries by total weight and select top 5
top_countries = total_weight_by_country.sort_values(by="Weight", ascending=False).head(5)["Country"]
# Step 3: Filter the data for the top 5 countries
df_comparison_top_5 = df[df["Country"].isin(top_countries)]
# Step 4: Create the line chart
fig_comparison_top_5 = px.line(
df_comparison_top_5,
x="Year",
y="Weight",
color="Country",
title="Yearly Weight-for-Height Comparison of Top 5 Countries"
)
fig_comparison_top_5.update_layout(
xaxis_title="Year",
yaxis_title="Weight-for-Height (Thousands)",
height=500
)
# Show the plot
fig_comparison_top_5.show()df_meta1 = pd.read_csv("unicef_metadata.csv")This scatter plot visualizes the relationship between GDP per capita (constant 2015 US$) and Life Expectancy (years) for the top 5 countries with the highest GDP per capita. The countries displayed are Bermuda, Liechtenstein, Luxembourg, United Arab Emirates, and Switzerland. The plot shows a generally positive correlation, where countries with higher GDP per capita tend to have higher life expectancy, although there are some deviations, particularly with Liechtenstein and United Arab Emirates.
# Sort the dataset by GDP per capita in descending order
df_sorted_gdp = df_meta1.sort_values(by="GDP per capita (constant 2015 US$)", ascending=False)
# Select the top 5 countries based on GDP
top_5_gdp_countries = df_sorted_gdp["country"].unique()[:5]
# Filter the dataset to include only the top 5 countries
df_top_5_gdp = df_meta1[df_meta1["country"].isin(top_5_gdp_countries)]
# Filter out rows with missing GDP or Life Expectancy data
df_top_5_gdp = df_top_5_gdp.dropna(subset=["GDP per capita (constant 2015 US$)", "Life expectancy at birth, total (years)"])
# Create a scatter plot for GDP vs Life Expectancy for the top 5 countries
fig_gdp_life_top5 = px.scatter(
df_top_5_gdp,
x="GDP per capita (constant 2015 US$)",
y="Life expectancy at birth, total (years)",
color="country",
hover_data=["year", "country"],
title="Top 5 Countries: GDP per Capita vs Life Expectancy"
)
fig_gdp_life_top5.update_layout(
xaxis_title="GDP per Capita (constant 2015 US$)",
yaxis_title="Life Expectancy (Years)",
height=500
)
fig_gdp_life_top5.show()In this analysis, several data transformations were carried out to prepare the data for visualization and analysis. These transformations helped in cleaning, merging, and restructuring the data to extract meaningful insights. Below is a summary of the key steps involved in transforming the data.
This analysis explored the evolving relationship between childhood obesity (measured as Weight-for-Height) and life expectancy across multiple countries over the period from 2000 to 2022. By analyzing global trends and comparing key countries, several key insights emerged:
Rising Childhood Obesity: There is a global increase in the number of overweight children, particularly in developed countries. Nations like the United States, China, and Australia saw a steady rise in Weight-for-Height, highlighting the growing concern of childhood obesity.
Weak Correlation with Life Expectancy: While life expectancy has generally increased across the globe, there is no clear linear correlation between life expectancy and the number of overweight children. In other words, higher life expectancy does not necessarily equate to fewer overweight children. This indicates that obesity and life expectancy are influenced by multiple factors, including diet, lifestyle, economic conditions, and healthcare systems.
Wealthier Nations and Obesity: The data suggests that wealthier nations tend to have both higher life expectancy and more significant childhood obesity. This may be due to changes in dietary habits, urbanization, and increased sedentary lifestyles, as well as increased availability of processed foods.
Diverse Global Trends: The relationship between life expectancy and childhood obesity shows significant variations across countries. While some countries with lower life expectancy show fewer overweight children, others exhibit high obesity rates despite similar life expectancy levels. This underscores the complexity of childhood obesity as a global health issue.
Focus on Prevention: To address rising childhood obesity, particularly in developed nations, public health interventions should focus on prevention. These could include promoting healthier diets, physical activity, and reducing sedentary behavior in children.
Targeting Urban and Wealthier Regions: Given the link between economic development and rising childhood obesity, initiatives targeting urban areas and wealthier regions should be prioritized. Efforts to improve access to nutritious food and physical activity opportunities are crucial.
Improved Healthcare and Nutrition: In emerging economies, improving healthcare systems and providing better access to nutrition could help mitigate the rising rates of obesity. Early interventions, including education on healthy lifestyles and better access to healthcare, are essential to curbing the obesity epidemic.
Cross-Country Collaboration: International collaborations between countries facing similar challenges with childhood obesity could help share effective strategies. Learning from countries with successful obesity prevention programs can help develop tailored interventions for countries with higher obesity rates.
Long-term Monitoring: Continuous monitoring of childhood obesity trends should be done to assess the effectiveness of interventions and adapt strategies as necessary. Regular data collection on Weight-for-Height and life expectancy will be key in understanding the evolving global health challenges.
This work underscores the growing global concern of childhood obesity, which is increasing despite rising life expectancy in many countries. Addressing this issue requires comprehensive strategies that go beyond healthcare and nutrition, including addressing lifestyle factors, urbanization, and the impact of economic development. By focusing on prevention and early intervention, countries can work towards reducing childhood obesity rates and improving overall child health.